﻿<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RequestDemo</title>
</head>
<body>
    <button id="btn">发起请求</button>

    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
    <script>
        //日期格式化
        (function () {

            if (Date.prototype.format) return;

            Date.prototype.format = function (fmt) { //author: meizz 
                var o = {
                    "M+": this.getMonth() + 1, //月份 
                    "d+": this.getDate(), //日 
                    "h+": this.getHours(), //小时 
                    "m+": this.getMinutes(), //分 
                    "s+": this.getSeconds(), //秒 
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
                    "S": this.getMilliseconds() //毫秒 
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            };

        })();

        var config = {
            //应用Id
            appId: "temp_test",
            //应用密钥
            appSecret: "da22bc9f-8c09-4b8d-96cc-c147b5e0f741"
        };
        //接口根地址
        var rootUrl = 'http://hardware.parking.cnool.net';

        $('#btn').click(function () {
            var businessParam = {
                lockId: '999995'
            };

            $.ajax({
                type: "post",
                url: rootUrl + '/Api/Tcp/LockIsOk',
                data: buildRequestParam(businessParam),
                dataType: "json",
                success: function (resJson) {
                    if (resJson.Success)
                        alert('请求成功！');
                    else
                        alert('请求失败！' + resJson.Msg);
                }
            });
        });

        //构建请求参数，使用默认签名
        function buildRequestParam(businessParam) {
            var appId = config.appId;
            var appSecret = config.appSecret;

            var requestParam = {};
            Object.keys(businessParam).forEach(function (aKey) {
                requestParam[aKey] = businessParam[aKey];
            });
            requestParam["appId"] = appId;
            requestParam["time"] = (new Date()).format("yyyy-MM-dd hh:mm:ss");

            var signBuilder = "";
            Object.keys(requestParam).sort().forEach(function (aKey) {
                var value = "";
                if (requestParam[aKey]) {
                    value = requestParam[aKey];
                }
                signBuilder += (aKey + value)
            });
            signBuilder += appSecret;
            var sign = md5(signBuilder).toUpperCase();
            requestParam["sign"] = sign;

            return requestParam;
        }
    </script>
</body>
</html>